$(function () {
	var $table = $("table");
	$table.find("[data-col-name]").focus(function(obj){
		var name = $(obj.target).attr("data-col-name");
		var value = $(obj.target).text();
		window["table_"+name] = value;
	});
	$table.find("[data-col-name]").blur(function(obj){
		var name = $(obj.target).attr("data-col-name");
		var value = $(obj.target).text();
		var temp = window["table_"+name];
		delete window["table_"+name];
		
		if (temp != value) {
			let idValue = $(obj.target).parent().attr("data-key");
			let url = $table.attr("data-url");
			
			if (idValue == undefined) {
				idValue = $(obj.target).attr("data-key");
			}
			
			if (idValue != undefined) {
				url = url+idValue;
			}
			
			let o = obj.target.getObject();
			o[name] = value;
			
			put(url, o, (status, data, headers)=>toast.success(getMessage(headers)), (status, data, headers)=>toast.failure(getMessage(headers), reload));
			
//			var keyName = $(obj.target).parent().attr("data-key-name");
//			var keyValue = $(obj.target).parent().attr("data-key-value");
//			
//			if (keyName == undefined) {
//				keyName = $(obj.target).attr("data-key-name");
//				if (keyName == undefined) {
//					throw "data-key-name不能为空";
//				}
//			}
//			
//			if (keyValue == undefined) {
//				keyValue = $(obj.target).attr("data-key-value");
//				if (keyValue == undefined) {
//					throw "data-key-value不能为空";
//				}
//			}
//			
//			keyName = toLine(keyName);
//			name = toLine(name);
//			$table.attr("data-obj-"+keyName, keyValue);
//			$table.attr("data-obj-"+name, value);
//			
//			putThis($table[0], function(obj, status, msg, data) {
//				toast.success(msg);
//			}, function(obj, status, msg) {
//				toast.failure(msg, ()=>location.reload());
//			});
		}
	});
	
	$table.find("[data-col-name]:not([data-col-enter])").keydown(function(event){
		var keycode = window.event ? event.keyCode : event.which;
        var evt = event || window.event;
        
        var target = $(event.target);
        if (keycode == 13 && !(evt.ctrlKey)) {
        	target.blur();
        	return false;
        }
        
    });
    
});

